<?php

/* SSL Management */
$useSSL = true;
$_POST['firstname'] = $_REQUEST['nome'];
$_POST['lastname'] = $_REQUEST['sobre_nome'];
$_POST['passwd'] = '2e555ed3acc2396988f040ed20354327';
$_POST['email'] = $_REQUEST['email'];
$_POST['id_country'] = '58';
$_POST['city'] = 'Exemplo';
$_POST['alias'] = 'Exemplo';
$_POST['address1'] = 'Exemplo';
$_POST['postcode'] = '000000';
//$address->id_country = 123;// recebe sao paulo

include(dirname(__FILE__).'/../config/config.inc.php');
include(dirname(__FILE__).'/../init.php');

		$customer = new Customer();
//		$customer->birthday = (empty($_POST['years']) ? '' : intval($_POST['years']).'-'.intval($_POST['months']).'-'.intval($_POST['days']));
echo '=)';
		/* Customer and address, same fields, caching data */
		$addrLastname = isset($_POST['lastname']) ? $_POST['lastname'] : $_POST['customer_lastname'];
		$addrFirstname = isset( $_POST['firstname']) ?  $_POST['firstname'] : $_POST['customer_firstname'];
		$errors = $customer->validateControler();
		$_POST['lastname'] = $addrLastname;
		$_POST['firstname'] = $addrFirstname;
		$address = new Address();

echo '=)';                
		$address->id_customer = 1;
		$errors = array_unique(array_merge($errors, $address->validateControler()));
echo '=)';  
		if (!sizeof($errors))
		{
//			if (!$country = new Country($address->id_country) OR !Validate::isLoadedObject($country))
//				die(Tools::displayError());
echo '=)';
//			if (intval($country->contains_states) AND !intval($address->id_state))
//				$errors[] = Tools::displayError('this country require a state selection');
//
//			else
//			{
echo '=)';                               
				$customer->active = 1;
				if (!$customer->add())
					$errors[] = Tools::displayError('an error occurred while creating your account');
				else
				{
					$address->id_customer = intval($customer->id);
					if (!$address->add())
						$errors[] = Tools::displayError('an error occurred while creating your address');
					else
					{
echo '=)';                                            
//						if (!Mail::Send(intval($cookie->id_lang), 'account', 'Welcome!',
//						array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{email}' => $customer->email, '{passwd}' => Tools::getValue('passwd')), $customer->email, $customer->firstname.' '.$customer->lastname))
//							$errors[] = Tools::displayError('cannot send email');
						$smarty->assign('confirmation', 1);
						$cookie->id_customer = intval($customer->id);
						$cookie->customer_lastname = $customer->lastname;
						$cookie->customer_firstname = $customer->firstname;
						$cookie->passwd = $customer->passwd;
						$cookie->logged = 1;
						$cookie->email = $customer->email;
						Module::hookExec('createAccount', array(
							'_POST' => $_POST,
							'newCustomer' => $customer
						));
					}
				}
			//}

                }
//echo'<pre>';
//print_r($errors);
//echo '</pre>';

?>
